package com.ms.game.characterInfo.db;

import java.sql.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import com.xpec.c4.db.basic.BasicLogDAO;
import com.xpec.c4.db.hibernate.manager.HibernateLogManager;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.module.datacenter.player.db.DailyExchangeLogModel;

/**
 * 玩家基本資料第一頁面所有資訊
 *
 */
public class DailyExchangeLogDAO extends BasicLogDAO {

	private static Log log = LogFactory.getLog(DailyExchangeLogDAO.class);
	
	/**
	 * @param playerID
	 * @return
	 * 獲得今日遊戲記錄log
	 */
  /*@SuppressWarnings("unchecked")
	public static List<BaseGameInfoLogModel> loadTodayGameInfoByGuid(GUID playerID) {
		if (playerID == null) {
			return null;
		}
		Session session = HibernateLogManager.getSession();
		Query query = session.createQuery("from TodayGameInfoLog where playerGUID = :guid");
		query.setParameter("guid", playerID.toString());

		List<BaseGameInfoLogModel> list = (List<BaseGameInfoLogModel>) query.list();
		HibernateLogManager.endSession();
		return list;
	}*/
	
	/**
	 * @param playerID
	 * @return
	 * 獲得今日押注和今日得分
	 */
	public static DailyExchangeLog loadDailyExchangeByGuid(GUID playerID , Date date) {
		if (playerID == null) {
			return null;
		}
		Session session = HibernateLogManager.getSession();
		Query query = session.createQuery("from DailyExchangeLog where playerID = :guid and eventDate = :date");
		query.setParameter("guid", playerID.toString());
		query.setParameter("date", date);

		DailyExchangeLog model = (DailyExchangeLog) query.uniqueResult();
		HibernateLogManager.endSession();
		return model;
	}
	
	/**
	 * @param playerID
	 * @return
	 * 時間查詢遊戲記錄log
	 */
	@SuppressWarnings("unchecked")
	public static List<DailyExchangeLogModel> loadDailyExchangeByGuidAndDateTime(GUID playerID , Date date) {
		if (playerID == null) {
			return null;
		}
		
		String dailyExchangeModelName = DailyExchangeLogModel.class.getSimpleName();
		Session session =HibernateLogManager.getLogSelectSession(dailyExchangeModelName,date); //HibernateLogManager.getSession();
		Query query = session.createQuery("from "+dailyExchangeModelName+" where playerID = :guid and eventDate = :date order by gameTime desc");
		query.setParameter("guid", playerID.toString());
		query.setParameter("date", date);

		List<DailyExchangeLogModel> list = (List<DailyExchangeLogModel>) query.list();
		HibernateLogManager.endSession();
		return list;
	}
	
	/**
	 * 更新每日bet數和score數
	 */
	public static void saveOrUpdateDailyExchange(GUID playerID, GUID accountID, String name, long bet , long score){
		
		//今天日期
		Date date= new Date(TimeSys.currentTimeMills());
		
		Session session = HibernateLogManager.txBegin();
		Query query = session.createQuery("from DailyExchangeLog where playerID = :playerID and eventDate = :date");
		query.setParameter("playerID", playerID.toString());
		query.setParameter("date", date);
		DailyExchangeLog model = (DailyExchangeLog) query.uniqueResult();
		if(model == null){
			model = new DailyExchangeLog();
			model.playerID = playerID.toString();
			session.save(model);
		}else{
			session.update(model);
		}
		
		HibernateLogManager.txCommitFailRollbackClearSession();
	}
	
}
